/**   
* @Title: MyBatisConfig.java
* @Package com.jootu.cc.ccapi
* @Description: TODO(用一句话描述该文件做什么)
* @author antch,861710865@qq.com   
* @date 2017年8月11日 下午9:25:57
* @version V1.0   
*/

package com.jootu.cc.ccapi;

import java.sql.SQLException;
import java.util.Arrays;
/*import java.util.Properties;

import org.apache.ibatis.plugin.Interceptor;*/
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.util.StringUtils;
//import com.github.pagehelper.PageInterceptor;


/**
 * @ClassName: MyBatisConfig
 * @Description: TODO(MyBatis集成分页插件)
 * @author antch,861710865@qq.com
 * @date 2017年8月11日 下午9:25:57
 * 
 */

@Configuration
@EnableTransactionManagement
@MapperScan("com.jootu.cc.ccapi.domain")//扫描接口
public class MyBatisConfig implements EnvironmentAware {

	private Environment environment;

	private RelaxedPropertyResolver propertyResolver;

	@Override
	public void setEnvironment(Environment environment) {
		// TODO Auto-generated method stub
		this.environment = environment;
		this.propertyResolver = new RelaxedPropertyResolver(environment, "spring.datasource.");
	}

	@Bean(initMethod = "init", destroyMethod = "close")
	public DruidDataSource datasource() throws SQLException {
		if (StringUtils.isEmpty(propertyResolver.getProperty("url"))) {
			System.out.println("Your database connection pool configuration is incorrect!"
					+ " Please check your Spring profile, current profiles are:"
					+ Arrays.toString(environment.getActiveProfiles()));
			// 抛出异常
			throw new ApplicationContextException("Database connection pool is not configured correctly");
		}
		DruidDataSource druidDataSource = new DruidDataSource();

		druidDataSource.setDriverClassName(propertyResolver.getProperty("driver-class-name"));
		druidDataSource.setUrl(propertyResolver.getProperty("url"));
		druidDataSource.setUsername(propertyResolver.getProperty("username"));
		druidDataSource.setPassword(propertyResolver.getProperty("password"));
		druidDataSource.setInitialSize(Integer.parseInt(propertyResolver.getProperty("initialSize")));
		druidDataSource.setMinIdle(Integer.parseInt(propertyResolver.getProperty("minIdle")));
		druidDataSource.setMaxActive(Integer.parseInt(propertyResolver.getProperty("maxActive")));
		druidDataSource.setMaxWait(Integer.parseInt(propertyResolver.getProperty("maxWait")));
		druidDataSource.setTimeBetweenEvictionRunsMillis(
				Long.parseLong(propertyResolver.getProperty("timeBetweenEvictionRunsMillis")));
		druidDataSource.setMinEvictableIdleTimeMillis(
				Long.parseLong(propertyResolver.getProperty("minEvictableIdleTimeMillis")));
		druidDataSource.setValidationQuery(propertyResolver.getProperty("validationQuery"));
		druidDataSource.setTestWhileIdle(Boolean.parseBoolean(propertyResolver.getProperty("testWhileIdle")));
		druidDataSource.setTestOnBorrow(Boolean.parseBoolean(propertyResolver.getProperty("testOnBorrow")));
		druidDataSource.setTestOnReturn(Boolean.parseBoolean(propertyResolver.getProperty("testOnReturn")));
		druidDataSource.setPoolPreparedStatements(
				Boolean.parseBoolean(propertyResolver.getProperty("poolPreparedStatements")));
		druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(
				Integer.parseInt(propertyResolver.getProperty("maxPoolPreparedStatementPerConnectionSize")));
		druidDataSource.setFilters(propertyResolver.getProperty("filters"));

		return druidDataSource;

	}
	
	@Bean
	public SqlSessionFactory sqlSessionFactory() throws Exception {
		SqlSessionFactoryBean bean  = new SqlSessionFactoryBean();
		bean.setDataSource(datasource());
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		//resolver.getResources("classpath:/mapper/*.xml")
        //扫描到MyBatis的mapper文件
		bean.setMapperLocations(resolver.getResources("classpath:/mapper/*.xml"));
		//添加分页
		/*PageInterceptor pageInterceptor = new PageInterceptor();
		Properties pro = new Properties();
		pro.setProperty("helperDialect", "mysql");
		pro.setProperty("reasonable", "true");
		pro.setProperty("supportMethodsArguments", "true"); 
		pro.setProperty("returnPageInfo", "check"); 
		pro.setProperty("params", "count=countSql"); 
		pro.setProperty("autoRuntimeDialect", "true");
		
		//添加插件
		pageInterceptor.setProperties(pro);
		bean.setPlugins(new Interceptor[] {pageInterceptor});*/
		
		return bean.getObject();
		
	}
	
	@Bean
	public PlatformTransactionManager transactionManager() throws SQLException {
		return new DataSourceTransactionManager(datasource());
	}
}
